package webmarket.common;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;

public class MarkerController {
	public static enum MarkerTypes {
		BAD_REQUEST, LOGIN, USERS, FORBIDDEN, ERRORS, INTERNAL_ERROR, LOGOUT, CREATE, SELL, NOT_FOUND, SHOP, REGISTER, LOGIN_INVALID
	}

	private MarkerController() {
	}

	private static final Map<MarkerTypes, String> markerNames = new HashMap<MarkerTypes, String>();
	private static final Map<MarkerTypes, Marker> markers = new HashMap<MarkerTypes, Marker>();

	private static void createRoot(MarkerTypes root) {
		getMarkers().put(root, MarkerManager.getMarker(root.toString()));
	}

	private static void createInheritence(MarkerTypes child, MarkerTypes parent) {
		getMarkers().put(child, MarkerManager.getMarker(child.toString(), parent.toString()));
		if (!markerTree.containsKey(parent)) {
			markerTree.put(parent, new LinkedList<MarkerTypes>());
		}
		markerTree.get(parent).add(child);
	}

	/**
	 * @return the roots
	 */
	public static MarkerTypes[] getRoots() {
		return roots;
	}
	
	public static Marker getMarker(MarkerTypes type){
		return getMarkers().get(type);
	}
	
	public static String getName(MarkerTypes type){
		return getMarkernames().get(type);
	}

	/**
	 * @return the markers
	 */
	public static Map<MarkerTypes, Marker> getMarkers() {
		return markers;
	}

	/**
	 * @return the markernames
	 */
	public static Map<MarkerTypes, String> getMarkernames() {
		return markerNames;
	}

	private static final MarkerTypes[] roots = new MarkerTypes[] { MarkerTypes.ERRORS, MarkerTypes.USERS, MarkerTypes.SHOP };
	private static final Map<MarkerTypes, List<MarkerTypes>> markerTree = new HashMap<MarkerTypes, List<MarkerTypes>>();

	static {
		getMarkernames().put(MarkerTypes.ERRORS, "Hibák");
		getMarkernames().put(MarkerTypes.BAD_REQUEST, "Hibás lekérés");
		getMarkernames().put(MarkerTypes.INTERNAL_ERROR, "Szerver oldali hiba");
		getMarkernames().put(MarkerTypes.NOT_FOUND, "Nem található");
		getMarkernames().put(MarkerTypes.FORBIDDEN, "Nem engedélyezett");

		getMarkernames().put(MarkerTypes.USERS, "Felhasználó");
		getMarkernames().put(MarkerTypes.LOGIN, "Bejelentkezés");
		getMarkernames().put(MarkerTypes.REGISTER, "Új felhasználó");
		getMarkernames().put(MarkerTypes.LOGIN_INVALID, "Piactér");
		getMarkernames().put(MarkerTypes.LOGOUT, "Kijelentkezés");

		getMarkernames().put(MarkerTypes.SHOP, "Piactér");
		getMarkernames().put(MarkerTypes.CREATE, "Létrehozás");
		getMarkernames().put(MarkerTypes.SELL, "Megvétel");

		for (MarkerTypes root : roots) {
			createRoot(root);
		}

		createInheritence(MarkerTypes.BAD_REQUEST, MarkerTypes.ERRORS);
		createInheritence(MarkerTypes.INTERNAL_ERROR, MarkerTypes.ERRORS);
		createInheritence(MarkerTypes.NOT_FOUND, MarkerTypes.ERRORS);
		createInheritence(MarkerTypes.FORBIDDEN, MarkerTypes.ERRORS);

		createInheritence(MarkerTypes.LOGIN, MarkerTypes.USERS);
		createInheritence(MarkerTypes.REGISTER, MarkerTypes.USERS);
		createInheritence(MarkerTypes.LOGIN_INVALID, MarkerTypes.USERS);
		createInheritence(MarkerTypes.LOGOUT, MarkerTypes.USERS);

		createInheritence(MarkerTypes.CREATE, MarkerTypes.SHOP);
		createInheritence(MarkerTypes.SELL, MarkerTypes.SHOP);
	}
}
